# 导入模块  （初始化 布局 参数）
# 自定义函数
"""
球（）
    初始化函数
    移动函数
    判断球拍打到球 ---作用在球 移动函数上
球拍 （）
    初始化函数
    移动函数  控制  键盘
分数（）
    初始化函数
    分数变化函数
"""
# 程序主入口
import time, random  # 导入 时间 动画
from tkinter import *


class Ball():
    def __init__(self, canvas, paddle, score, color):
        self.canvas = canvas
        self.score = score
        self.paddle = paddle  # 球拍
        self.id = canvas.create_oval(0, 0, 50, 50, fill=color)  # 大小
        self.canvas.move(self.id, 400, 300)
        self.x = -3
        self.y = 3
        self.game_over = False

    def draw(self):
        pos = self.canvas.coords(self.id)  # pos = [x1,y1,x2,y2]
        canvas.move(self.id, self.x, self.y)
        if pos[0] <= 0:  # x1  移动状态
            self.x = 3
        if pos[2] >= 800:
            self.x = -3
        if pos[1] <= 0:
            self.y = 3
        if pos[3] >= 600:
            self.game_over = True
            # self.y = -3 # 游戏结束的值 = True
        if self.hit_paddle(pos) == True:
            self.y = -3

    def hit_paddle(self, pos):  # 满足水平和垂直方向， 把小球和球拍的坐标进行比较
        paddle_pos = self.canvas.coords(self.paddle.id)  # 球拍的坐标
        # 小球的X2 大于球拍的X1 , 球拍的X2 大于 小球的X1
        if pos[2] >= paddle_pos[0] and paddle_pos[2] >= pos[0]:
            if pos[3] >= paddle_pos[1] and paddle_pos[3] >= pos[3]:  # 球拍的y2 在小球y2下面  静止
                self.score.hit()
                self.x = self.x + self.paddle.x
                return True
        return False


class Paddle():
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 500, 50, fill=color)  # 大小
        self.canvas.move(self.id, 200, 500)
        self.x = -3
        self.y = 0
        self.game_started = False
        self.canvas.bind_all("<KeyPress-Left>", self.turn_left)
        self.canvas.bind_all("<KeyPress-Right>", self.turn_right)
        self.canvas.bind_all("<KeyPress-F1>", self.started)

    def started(self, evt):
        self.game_started = True

    def turn_left(self, evt):
        self.x = -5

    def turn_right(self, evt):
        self.x = 5

    def draw(self):
        pos = self.canvas.coords(self.id)
        canvas.move(self.id, self.x, self.y)
        if pos[0] <= 0:  # x1  移动状态
            self.x = 3
        if pos[2] >= 800:
            self.x = -3
        if pos[1] <= 0:
            self.y = 3
        if pos[3] >= 600:
            self.y = -3


class Score():
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.score = 0
        self.id = canvas.create_text(700, 50, text=self.score, font=("Arial", 30), fill=color)  # 位置

    def hit(self):
        self.score = self.score + 3
        self.canvas.itemconfig(self.id, text=self.score)  # 事件更新


tk = Tk()  # 实例化 动画
tk.title("写第二遍")
# tk.wm_attributes('-topmost',1)
canvas = Canvas(tk, width=800, height=600)  #
canvas.pack()
game_over_text = canvas.create_text(450, 300, text="game_over", font=("Arial", 30), state = "hidden")
paddle = Paddle(canvas, "blue")
#   def __init__(self,canvas,paddle,color):
score = Score(canvas, "red")
ball = Ball(canvas, paddle, score, "red")

while 1:
    if paddle.game_started == True and ball.game_over == False:  # 按下左键 值(stared) 为 True 则 执行 draw().  游戏没开始的时候 值 = False
        ball.draw()  # 游戏运行
        paddle.draw()
    if ball.game_over == True:
        canvas.itemconfig(game_over_text, state = "normal")
    canvas.update()
    canvas.update_idletasks()
    time.sleep(0.01)

"""
import tkinter
tk = tkinter.Tk()
tkinter.Canvas.create
"""
